﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.IO;

namespace VehicleRecordSystem.view
{
    /// <summary>
    /// Interaction logic for VehicleInsurancePage.xaml
    /// </summary>
    public partial class VehicleInsurancePage : Page
    {
        private static VehicleInsurancePage instanse;
        private string errorBackgroundColor = "#EBA4B0";

        public static VehicleInsurancePage Instanse
        {
            get
            {
                if (instanse == null)
                {
                    instanse = new VehicleInsurancePage();
                }

                return instanse;
            }
        }

        public VehicleInsurancePage()
        {
            InitializeComponent();
        }

        private void btnInsurancePageSave_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                String policy_number = txtPolicyNumber.Text;
                String company = txtCompany.Text;
                String owner_name = txtOwnerName.Text;
                String owner_address = txtOwnerAddress.Text;
                DateTime? from_date = datePickerFromDate.SelectedDate;
                DateTime? to_date = datePickerToDate.SelectedDate;
                DateTime? issue_date = datePickerIssueDate.SelectedDate;
                BitmapImage image = new BitmapImage(new Uri(txtImagePath.Text));
            }
        }

        private void btnImageBrowse_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.ShowDialog();
            openFileDialog.Title = "Set Insurance Card Image";
            openFileDialog.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + "Portable Network Graphic (*.png)|*.png";
            openFileDialog.DefaultExt = ".jpeg";
            txtImagePath.Text = openFileDialog.FileName;
        }

        private Boolean Validate()
        {
            Boolean ok=true;
            var bc = new BrushConverter();

            if (string.IsNullOrEmpty(txtPolicyNumber.Text))
            {
                ok = false;
                txtPolicyNumber.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(txtCompany.Text))
            {
                ok = false;
                txtCompany.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if(string.IsNullOrEmpty(txtOwnerName.Text))
            {
                ok=false;
                txtOwnerName.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if(string.IsNullOrEmpty(txtOwnerAddress.Text))
            {
                ok=false;
                txtOwnerAddress.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if(string.IsNullOrEmpty(txtImagePath.Text))
            {
                ok=false;
                txtImagePath.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (!validUri(txtImagePath.Text))
            {
                ok = false;
                txtImagePath.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(datePickerFromDate.SelectedDate.ToString()))
            {
                ok = false;
                datePickerFromDate.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(datePickerToDate.SelectedDate.ToString()))
            {
                ok = false;
                datePickerToDate.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(datePickerIssueDate.SelectedDate.ToString()))
            {
                ok = false;
                datePickerIssueDate.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }            
            return ok; 
        }
        private Boolean validUri(String path)
        {
            Boolean ok=true;
            try
            {
                new Uri(path);
            }
            catch
            {
                ok = false;
            }
            return ok;
        }

        private void changeBackgroundColorToNormal(object sender, RoutedEventArgs e)
        {
            var bc = new BrushConverter();

            if (sender is TextBox)
            {
                TextBox txt = (TextBox)sender;
                txt.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
            if (sender is DatePicker)
            {
                DatePicker dp = (DatePicker)sender;
                dp.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
        }
        
    }
}
